/**
 * 简化的单例模式实现
 * 
 * 使用装饰器和辅助函数，而非继承，避免 TypeScript 类型问题
 * 
 * @author AI Assistant
 * @date 2025-11-27
 */

import { Log } from '../logger/LoggerGlobal';

/**
 * 单例注册表
 */
class SingletonRegistry {
    private static instances: Map<string, any> = new Map();
    
    static set(name: string, instance: any): void {
        this.instances.set(name, instance);
    }
    
    static get(name: string): any {
        return this.instances.get(name);
    }
    
    static has(name: string): boolean {
        return this.instances.has(name);
    }
    
    static delete(name: string): void {
        const instance = this.instances.get(name);
        if (instance && typeof instance.destroy === 'function') {
            instance.destroy();
        }
        this.instances.delete(name);
    }
    
    static clear(): void {
        for (const [name, instance] of this.instances) {
            if (typeof instance.destroy === 'function') {
                instance.destroy();
            }
        }
        this.instances.clear();
    }
    
    static getAll(): Map<string, any> {
        return new Map(this.instances);
    }
}

/**
 * 单例装饰器（简化版）
 * 
 * 使用示例：
 * ```typescript
 * @singleton
 * export class MyManager {
 *     private constructor() {}
 *     static getInstance(): MyManager { return null as any; }
 * }
 * ```
 */
export function singleton<T extends { new (...args: any[]): any }>(constructor: T) {
    let instance: any = null;
    const className = constructor.name;
    
    return class extends constructor {
        constructor(...args: any[]) {
            super(...args);
            if (!instance) {
                instance = this;
                SingletonRegistry.set(className, instance);
                Log.general.debug('单例已创建', { className });
            }
            return instance;
        }
        
        static getInstance(): InstanceType<T> {
            if (!instance) {
                instance = new this();
            }
            return instance;
        }
        
        static resetInstance(): void {
            if (instance) {
                SingletonRegistry.delete(className);
                instance = null;
            }
        }
        
        static hasInstance(): boolean {
            return instance !== null;
        }
    } as any;
}

/**
 * 单例管理器（简化版）
 */
export const SingletonManager = {
    /**
     * 销毁所有单例
     */
    destroyAll(): void {
        Log.general.info('销毁所有单例');
        SingletonRegistry.clear();
    },
    
    /**
     * 获取单例
     */
    get(name: string): any {
        return SingletonRegistry.get(name);
    },
    
    /**
     * 检查单例是否存在
     */
    has(name: string): boolean {
        return SingletonRegistry.has(name);
    },
    
    /**
     * 删除单例
     */
    delete(name: string): void {
        SingletonRegistry.delete(name);
    },
    
    /**
     * 获取所有单例
     */
    getAll(): Map<string, any> {
        return SingletonRegistry.getAll();
    },
    
    /**
     * 打印统计信息
     */
    printStatistics(): void {
        const instances = SingletonRegistry.getAll();
        
        Log.styled('━'.repeat(60), 'separator');
        Log.styled('📊 单例管理器统计', 'title');
        Log.styled('━'.repeat(60), 'separator');
        
        Log.general.info(`单例总数: ${instances.size}`);
        
        if (instances.size > 0) {
            Log.styled('📋 已注册的单例', 'large');
            for (const name of instances.keys()) {
                Log.general.info(`  - ${name}`);
            }
        }
        
        Log.styled('━'.repeat(60), 'separator');
    }
};

/**
 * 使用示例
 * 
 * ```typescript
 * export class MyManager {
 *     private static instance: MyManager | null = null;
 *     
 *     private constructor() {
 *         // 初始化
 *     }
 *     
 *     static getInstance(): MyManager {
 *         if (!this.instance) {
 *             this.instance = new MyManager();
 *         }
 *         return this.instance;
 *     }
 *     
 *     static resetInstance(): void {
 *         if (this.instance) {
 *             this.instance.destroy?.();
 *             this.instance = null;
 *         }
 *     }
 *     
 *     destroy(): void {
 *         // 清理资源
 *     }
 * }
 * 
 * // 使用
 * const manager = MyManager.getInstance();
 * ```
 */

